package service

import (
	"fmt"
	"time"

	"task/config"
	"task/models"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
	processChLen = promauto.NewGauge(prometheus.GaugeOpts{
		Name: fmt.Sprintf("%s_process_channel", config.Conf.TaskExecutor.Name),
		Help: "Length of task executor process channel.",
	})
	processTotal = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Name: fmt.Sprintf("%s_process_total", config.Conf.TaskExecutor.Name),
			Help: "Number of executor process",
		},
		[]string{"processor", "state"},
	)
	processDuration = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Name: fmt.Sprintf("%s_process_duration", config.Conf.TaskExecutor.Name),
			Help: "Duration of executor process",
		},
		[]string{"processor", "state"},
	)
)

// recordMetrics record metrics.
func recordMetrics(chanLen float64, start time.Time, event *models.ProcessEvent) {
	processChLen.Set(chanLen)
	duration := float64(time.Since(start)) / float64(time.Millisecond)
	processTotal.With(prometheus.Labels{
		"processor": fmt.Sprintf("%d", event.ProcessorId),
		"state":     fmt.Sprintf("%d", event.ProcessState),
	}).Inc()
	processDuration.With(prometheus.Labels{
		"processor": fmt.Sprintf("%d", event.ProcessorId),
		"state":     fmt.Sprintf("%d", event.ProcessState),
	}).Add(duration)
}
